package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.os.Handler;

import androidx.annotation.NonNull;

import com.gyf.immersionbar.ImmersionBar;
import com.hjq.base.BaseDialog;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.dialog.PrivacyAgreementConfirmDialog;
import com.qiantu.phone.utils.Constants;
import com.qiantu.phone.utils.SPUtils;

/**
 * time   : 2021/12/22
 * desc   : 闪屏界面
 */
public final class SplashActivity extends AppActivity {

    //    private LottieAnimationView mLottieView;
//    private SlantedTextView mDebugView;
    private Handler mHandler;
    private PrivacyAgreementConfirmDialog.Builder mPrivacyAgreementConfirmDialogBuilder;
    private BaseDialog mPrivacyAgreementConfirmDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.splash_activity;
    }

    @Override
    protected void initView() {
        overridePendingTransition(R.anim.anim_fade_in,R.anim.anim_fade_out);
//        mLottieView = findViewById(R.id.lav_splash_lottie);
//        mDebugView = findViewById(R.id.iv_splash_debug);
//        mDebugView.setText(AppConfig.getBuildType().toUpperCase());
//        if (AppConfig.isDebug()) {
//            mDebugView.setVisibility(View.VISIBLE);
//        } else {
//            mDebugView.setVisibility(View.INVISIBLE);
//        }
        // 设置动画监听
//        mLottieView.addAnimatorListener(new AnimatorListenerAdapter() {
//
//            @Override
//            public void onAnimationEnd(Animator animation) {
//                mLottieView.removeAnimatorListener(this);
//
//            }
//        });
    }

    @Override
    protected void initData() {
        if (SPUtils.getInstance().getBoolean(Constants.USER_PRIVACY_AGREEMENT_READ_KEY)) {
//            requestPermissions();
            toNext();
        } else {
            if(mPrivacyAgreementConfirmDialog == null){
                mPrivacyAgreementConfirmDialogBuilder = new PrivacyAgreementConfirmDialog.Builder(getContext());
                mPrivacyAgreementConfirmDialogBuilder.setOnConfirmClickListener(new PrivacyAgreementConfirmDialog.OnConfirmClickListener() {
                    @Override
                    public void onClick() {
                        SPUtils.getInstance().put(Constants.USER_PRIVACY_AGREEMENT_READ_KEY, true);
                        AppApplication.getInstance().initThirdNeedPrivacy();
                        mPrivacyAgreementConfirmDialog.dismiss();
                        toNext();
                    }
                });
                mPrivacyAgreementConfirmDialog = mPrivacyAgreementConfirmDialogBuilder.create();
            }
            mPrivacyAgreementConfirmDialog.show();
        }
    }

    private void toNext(){
        AppApplication.getInstance().initHouseData();
        mHandler = new Handler();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (AppApplication.getInstance().needGuid()){
                    GuideActivity.start(SplashActivity.this);
                    finish();
                }else {
                    AppApplication.getInstance().startSmartHome(SplashActivity.this);
                }
            }
        }, AppApplication.getInstance().openDelayTime);
    }

//    private void getCurrentHouse(){
//        EasyHttp.post(this)
//                .api(new GetCodeApi()
//                        .setData(data))
//                .request(new HttpCallback<HttpData<Void>>(this) {
//                    @Override
//                    public void onStart(Call call) {
//                    }
//
//                    @Override
//                    public void onEnd(Call call) {
//                        hideDialog();
//                    }
//
//                    @Override
//                    public void onSucceed(HttpData<Void> data) {
//                        if(data.isRequestSucceed()){
//
//                        }
//                    }
//
//                    @Override
//                    public void onFail(Exception e) {
//                        super.onFail(e);
//                    }
//                });
//    }

    @NonNull
    @Override
    protected ImmersionBar createStatusBarConfig() {
        return super.createStatusBarConfig()
                // 指定导航栏背景颜色
                .navigationBarColorInt(0xffffffff);
    }

    @Override
    public void onBackPressed() {
        //禁用返回键
        //super.onBackPressed();
    }

    @Override
    protected void initActivity() {
        // 问题及方案：https://www.cnblogs.com/net168/p/5722752.html
        // 如果当前 Activity 不是任务栈中的第一个 Activity
        if (!isTaskRoot()) {
            Intent intent = getIntent();
            // 如果当前 Activity 是通过桌面图标启动进入的
            if (intent != null && intent.hasCategory(Intent.CATEGORY_LAUNCHER)
                    && Intent.ACTION_MAIN.equals(intent.getAction())) {
                // 对当前 Activity 执行销毁操作，避免重复实例化入口
                finish();
                return;
            }
        }
        super.initActivity();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.anim_fade_in,R.anim.anim_fade_out);
    }

    @Deprecated
    @Override
    protected void onDestroy() {
        // 因为修复了一个启动页被重复启动的问题，所以有可能 Activity 还没有初始化完成就已经销毁了
        // 所以如果需要在此处释放对象资源需要先对这个对象进行判空，否则可能会导致空指针异常
        super.onDestroy();
    }
}